import axios from "axios";
import Vue from "vue";
import router from "../router";
import { Toast } from "vant";

//判断开发环境
if (process.env.NODE_ENV == 'development') {
    Vue.prototype.$pre = "http://localhost:2000"
}
if (process.env.NODE_ENV === 'production') {
    Vue.prototype.$pre = ""
}

//请求拦截
axios.interceptors.request.use(config => {
    // console.log("请求拦截");
    // console.log(config);
    // 如果是登录|注册，不加token;如果不是登录，也不是注册，加token
    if (config.url !== '/api/login' && config.url !== '/api/register') {
        config.headers.authorization = JSON.parse(localStorage.getItem("isLogin")).token;
        // console.log(config);
    }
    return config
})

//响应拦截
axios.interceptors.response.use(res => {
    // 统一打印数据
    // console.group("响应拦截,本次请求地址：" + res.config.url)
    // console.log(res);
    // console.groupEnd()

    // 统一处理失败
    if (res.data.code !== 200) {
        Toast(res.data.msg)
    }

    //统一处理掉线
    if (res.data.msg === '登录已过期或访问权限受限') {
        // 删除isLogin
        localStorage.removeItem("isLogin")
        //跳转到登录页面
        router.replace("/login")
    }
    return res;
})

//注册
export const reqRegister = (data)=>{
    return axios({
        url:"/api/register",
        method:"post",
        data,
    })
}

//login
export const reqLogin = (user) => {
    return axios({
        url: "/api/login",
        method: "post",
        data: user
    })
}

//搜索
export const reqSearch = (params)=>{
    return axios({
        url:"/api/search",
        method:"get",
        params,
    })
}

//首页的分类
export const reqHomeCate =()=>{
    return axios({
        url:"/api/getcate",
        method:"get",
    })
}

//首页的轮播
export const reqHomeBanner =()=>{
    return axios({
        url:"/api/getbanner",
        method:"get",
    })
}

//首页的商品
export const reqHomeGoods = ()=>{
    return axios({
        url:"/api/gethortgoods",
        method:"get"
    })
}

//详情页
export const reqDetail = (params)=>{
    return axios({
        url:"/api/getgoodsinfo",
        method:"get",
        params
    })
}

//加入购物车
export const reqAddShop =(data)=>{
    return axios({
        url:"/api/cartadd",
        method:"post",
        data,
    })
}

//所有分类
export const reqCates = () =>{
    return axios({
        url:"/api/getcates"
    })
}

//分类列表
export const reqCateList = (params)=>{
    return axios({
        url:"/api/getgoodlist",
        method:"get",
        params,
    })
}

//获取购物车
export const reqGetShop = (params)=>{
    return axios({
        url:"/api/cartlist",
        method:"get",
        params,
    })
}

//购物车删除
export const reqShopDel = (data)=>{
    return axios({
        url:"/api/cartdelete",
        method:"post",
        data,
    })
}
//购物车修改
export const reqShopEdit = (data)=>{
    return axios({
        url:"/api/cartedit",
        method:"post",
        data,
    })
}

//地址查询
export const reqAddressList = (params)=>{
    return axios({
        url:"/api/addresslist",
        method:"get",
        params,
    })
}

//地址添加
export const reqAddressAdd = (data)=>{
    return axios({
        url:"/api/addressadd",
        method:"post",
        data,
    })
}

//地址编辑
export const reqAddressEdit = (data)=>{
    return axios({
        url:"/api/addressedit",
        method:"post",
        data,
    })
}

//地址删除
export const reqAddressDel = (data)=>{
    return axios({
        url:"/api/addressremove",
        method:"post",
        data,
    })
}

//订单生成
export const reqOrderAdd = (data)=>{
    return axios({
        url:"/api/orderadd",
        method:"post",
        data,
    })
}